﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Citi.HeadCount.WebUI.Models;
using Citi.HeadCount.Domain;
using Citi.HeadCount.WebUI.Util;
using System.IO;

namespace Citi.HeadCount.WebUI.Controllers
{
    public class ReportController : Controller
    {
        private Dictionary<string, string> gocAttributesHeader = new Dictionary<string, string>
                {
                    {"GOC Attribute", "GOCAttribute"},
                    {"GOC(as number)", "GOCAsNumber"},
                    {"GOC Long Description", "LongDescription"},
                    {"Expense Code", "ExpenseCode"},
                    {"Corp Code", "CorpCode"},
                    {"GOC Status", "Status"},
                    {"Updated Date", "UpdatedDate"},
                    {"SID","SID"},
                    {"Local Cost Code", "LocalCostCode"},
                    {"Manager Id", "ManagerId"},
                    {"DSMT FRS BU", "FRSBU"},
                    {"FRS BU Long Description", "FRSBULongDescription"},
                    {"DSMT FRS OU", "FRSOU"},
                    {"DSMT FRS OU Description", "FRSOULongDescription"},
                    {"Function","Function"},
                    {"Function Short Description", "FunctionShortDescription"},
                    {"LVID", "LVID"},
                    {"LVID Long Description", "LVIDLongDescription"},
                    {"Managed Geography", "ManagedGeography"},
                    {"Managed Geography Description", "ManagedGeographyDescription"},
                    {"Managed Segment Id", "ManagedSegmentId"},
                    {"Managed Segment Long Description", "ManagedSegmentLongDescription"},
                    {TypeResultType.OTFlag.GetDescription(), TypeResultType.OTFlag.ToString()},
                    {TypeResultType.Business.GetDescription(), TypeResultType.Business.ToString()},
                    {TypeResultType.Business1.GetDescription(), TypeResultType.Business1.ToString()},
                    {TypeResultType.Area.GetDescription(), TypeResultType.Area.ToString()},
                    {TypeResultType.DeckRegional.GetDescription(), TypeResultType.DeckRegional.ToString()},
                    {TypeResultType.DeckRegional1.GetDescription(), TypeResultType.DeckRegional1.ToString()},
                    {TypeResultType.Local.GetDescription(), TypeResultType.Local.ToString()},
                    {TypeResultType.FPAManager.GetDescription(), TypeResultType.FPAManager.ToString()},
                    {TypeResultType.FPAAnalyst.GetDescription(), TypeResultType.FPAAnalyst.ToString()}
                };

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ShowReport()
        {
            var model = GOCAttributesViewModel.CreateModel(new Domain.Repository.RepositoryBase<GOC>().GetAll().ToList());
            TempData["GOCIndexModel"] = model;
            return RedirectToAction("Index", "GOC");
        }

        public ActionResult ShowExcelReport()
        {
            var model = GOCAttributesViewModel.CreateModel(new Domain.Repository.RepositoryBase<GOC>().GetAll().ToList());
            var functionsCount = model.Max(x => x.FunctionsLevels.Count());
            var managedSegmentCount = model.Max(x => x.ManagedSegmentLevels.Count());

            var gocAttributesHeaderList = gocAttributesHeader.ToList();
            var countEnumType = Enum.GetNames(typeof(TypeResultType)).Length;

            for (int i = 0; i < managedSegmentCount; i++)
                gocAttributesHeaderList.Insert(gocAttributesHeaderList.Count() - countEnumType, 
                    new KeyValuePair<string,string>(string.Concat("Managed Segment L", (i + 1)), string.Concat("ManagedSegmentLevels[", i, "]")));

            for (int i = 0; i < functionsCount; i++)
                gocAttributesHeaderList.Insert(gocAttributesHeaderList.Count() - countEnumType, 
                    new KeyValuePair<string,string>(string.Concat("Function L", (i + 1)), string.Concat("FunctionsLevels[", i, "]")));

            using (var stream = new ExcelGenerator().GerarArquivo(gocAttributesHeaderList.ToDictionary(c => c.Key, c => c.Value), model))
            {
                return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.csv");
            }
        }

    }
}
